package com.demo.zyt.modules.app.controller;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.demo.zyt.common.exception.RRException;
import com.demo.zyt.common.utils.AppResultEnum;
import com.demo.zyt.common.utils.PageUtils;
import com.demo.zyt.common.utils.R;
import com.demo.zyt.config.CommonParameters;
import com.demo.zyt.config.ConfigConstant;
import com.demo.zyt.modules.app.annotation.Login;
import com.demo.zyt.modules.app.entity.CollegeBaseEntity;
import com.demo.zyt.modules.app.entity.MajorEnrollmentEntity;
import com.demo.zyt.modules.app.service.CollegeBaseService;
import com.demo.zyt.modules.app.service.MajorEnrollmentService;

import lombok.extern.slf4j.Slf4j;

/**
 * 对应小程序“历年录取”的功能
 * @author ebusu
 *
 */
@RestController
@RequestMapping("/app/his")
@Slf4j
public class AppHistoryStudentController {
	
	@Autowired
	private MajorEnrollmentService majorEnrollmentService;
	
	@Autowired
	private CollegeBaseService collegeBaseService;
	
	/**
	 * 获取所有学校
	 * @param userId
	 * @param params
	 * @return
	 */
	@GetMapping("/query")
	@Login
	public R queryCollege(@RequestAttribute("userId") Integer userId, @RequestParam Map<String, Object> params) {
		
		log.info("params is {}" , params);
		
		// 招生计划, 只查询2018, 当前年份 - 1
		
		// "{"q2":"211","q9":"985","qy":"研究生院","qs":"双一流","batch":"本科二批","location2":"内蒙古","subject":"普通/普通文科","majorName":"majorNamemajorName","collegeName":"majorNamemajorNamemajorName"}"
		
		// params.put("year", (Calendar.getInstance().get(Calendar.YEAR) - 1));
		
		params.put("year", ConfigConstant.DEFAULT_QUERY_YEAR);
		
		if ( params.get("q2") != null && !StringUtils.isEmpty(String.valueOf(params.get("q2")).trim()) ) {
			
			params.put("q2", CommonParameters.IS_EFFECT.YES);
			
		} else {
			
			params.put("q2", null);
			
		}
		
		if ( params.get("q9") != null && !StringUtils.isEmpty(String.valueOf(params.get("q9")).trim())) {
			
			params.put("q9", CommonParameters.IS_EFFECT.YES);
			
		} else {
			
			params.put("q9", null);
			
		}
		
		if ( params.get("qy") != null && !StringUtils.isEmpty(String.valueOf(params.get("qy")).trim())) {
			
			params.put("qy", CommonParameters.IS_EFFECT.YES);
			
		} else {
			
			params.put("qy", null);
			
		}
		
		if ( params.get("qs") != null && !StringUtils.isEmpty(String.valueOf(params.get("qs")).trim())) {
			
			params.put("qs", CommonParameters.IS_EFFECT.YES);
			
		} else {
			
			params.put("qs", null);
			
		}
		
		if ( params.get("batch") != null && String.valueOf(params.get("batch")).equals("批次选择")) {
			
			params.put("batch", null);
			
		}
		
		if ( params.get("subject") != null && String.valueOf(params.get("subject")).equals("[\"科类选择\"]")) {
			
			params.put("subject", null);
			
		}
		
		if ( params.get("subject") != null && String.valueOf(params.get("subject")).equals("普通")) {
			
			params.put("subject", null);
			
		}
		
		if ( params.get("location2") != null && String.valueOf(params.get("location2")).equals("全国")) {
			
			params.put("location2", null);
			
		}
		
		if ( params.get("subject") != null ) {
			
			String subject = String.valueOf(params.get("subject"));
			
			subject = subject.substring(subject.indexOf("/") + 1);
			
			params.put("subject", subject);
			
		}
		
		PageUtils page = this.majorEnrollmentService.queryByCondition(params);
		
		return R.ok().put("page", page);
	}
	
	/**
	 * 获取某个学校下面的所有专业
	 * @param userId
	 * @param params
	 * @return
	 */
	@GetMapping("/query/major")
	@Login
	public R queryMajorByCollegeId(@RequestAttribute("userId") Integer userId, @RequestParam Map<String, Object> params) {
		
		log.info("params is {}", params);
		
		if ( params.get("collegeId") == null || params.get("subject") == null || params.get("batch") == null) {
			throw new RRException(AppResultEnum.USER_ARG_ERROR);
		}
		
		if ( params.get("subject") != null ) {
			
			String subject = String.valueOf(params.get("subject"));
			
			subject = subject.substring(subject.indexOf("/") + 1);
			
			params.put("subject", subject);
			
		}
		
		params.put("year", ConfigConstant.DEFAULT_QUERY_YEAR);
		
		List<MajorEnrollmentEntity> enrollmentEntities = this.majorEnrollmentService.queryMajorByCollegeId(params);
		
		CollegeBaseEntity collegeBaseEntity = this.collegeBaseService.selectById(params.get("collegeId").toString());
		
		String schoolImage = "http://www.gaokaozyt.cn/images/" + collegeBaseEntity.getCollegeId()+ "/headImage.jpg";
		
		return R.ok().put("enrollmentEntities", enrollmentEntities).put("collegeBaseEntity", collegeBaseEntity).put("schoolImage", schoolImage);
	}
	
	/**
	 * 获取某个学校、某个专业下的所有学生信息
	 * @param userId
	 * @param params
	 * @return
	 */
	@GetMapping("/query/student")
	@Login
	public R queryStudent(@RequestAttribute("userId") Integer userId, @RequestParam Map<String, Object> params) {
		
		if ( params.get("collegeId") == null || params.get("majorCode") == null || params.get("subject") == null
				|| params.get("batch") == null ) {
			throw new RRException(AppResultEnum.USER_ARG_ERROR);
		}
		
		params.put("year", ConfigConstant.DEFAULT_QUERY_YEAR);
		
		PageUtils page = this.majorEnrollmentService.queryStudent(params);
		
		return R.ok().put("page", page);
	}
	
}
